/******************************************************************************
 * @file     wdt_wwdt_reset_test.c
 * @version  V0.10
 * $Revision: 2 $
 * $Date: 21/03/10 2:50p $
 * @brief    IEC60730 Interrupt Test
 * @note
 * SPDX-License-Identifier: Apache-2.0
 * Copyright (C) 2016-2020 Nuvoton Technology Corp. All rights reserved.
 ******************************************************************************/

#include <stdio.h>
#include "nuc980.h"
#include "IEC60730_CONTROL_PARAM.h"
#include "IEC60730_SAFETY_TEST_LIB.h"

/*-------------------------------------------*/
/*           wdt reset test                  */
/*-------------------------------------------*/
void WDT_reset_test(void)
{
    /* unlock register */
    SYS_UnlockReg();

    /* clear WDT reset flag */
    WDT_CLEAR_RESET_FLAG();

    /* reset counter */
    WDT_RESET_COUNTER();

    /* set WDT time out period (16 ticks) and enable WDT/WDT_RST */
    WDT_Open(WDT_TIMEOUT_2POW4, WDT_RESET_DELAY_3CLK, TRUE, FALSE);

    /* lock register */
    SYS_LockReg();

    /* wait for WDT timeout */
    while(1);
}

/*-------------------------------------------*/
/*          wwdt reset test                  */
/*-------------------------------------------*/
void WWDT_reset_test(void)
{
    /* unlock register */
    SYS_UnlockReg();

    /* clear WWDT reset flag */
    WWDT_CLEAR_RESET_FLAG();

    /* reload counter */
    WWDT_RELOAD_COUNTER();

    /* set WWDT prescaler, compare data and enable WWDT/WWDT_INT */
    WWDT_Open(WWDT_PRESCALER_1, 0x80, TRUE);

    /* lock register */
    SYS_LockReg();

    /* wait for WWDT timeout */
    while(1);
}

/*-------------------------------------------*/
/*           wdt & wwdt reset test           */
/*-------------------------------------------*/
uint8_t IEC60730_WDT_WWDT_Reset_Test(void)
{
    uint8_t u8WDTTestPass;
#if 0
    /* check if WDT reset flag */
    if (!WDT_GET_RESET_FLAG())
    {
        printf("\n");
        printf("WDT no reset flag !!\n");
        printf("do WDT reset test !!\n");
        WDT_reset_test();
    }
#endif

    /* check if WWDT reset flag */
    if (!WWDT_GET_RESET_FLAG())
    {
        printf("\n");
        printf("WWDT no reset flag !!\n");
        printf("do WWDT reset test !!\n");
        WWDT_reset_test();
    }

    /* clear WDT & WWDT reset flag */
    SYS_UnlockReg();
    WDT_CLEAR_RESET_FLAG();
    WWDT_CLEAR_RESET_FLAG();
    SYS_LockReg();

    /* clear WDT & WWDT reset flag */
    u8WDTTestPass = TEST_PASS;
    return u8WDTTestPass;
}

/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
